﻿' --- Copyright (c) notice DnnConsulting.nl ---
'  Copyright (c) 2014 DnnConsulting.nl.  www.DnnConsulting.nl. BSD License.
' Author: G. M. Barlow
' ------------------------------------------------------------------------
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' ------------------------------------------------------------------------
' This copyright notice may NOT be removed, obscured or modified without written consent from the author.
' --- End copyright notice --- 
' 
Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Entities.Tabs
Imports DotNetNuke.Common.Utilities
Imports DotNetNuke.Common.Globals

Imports Google.GData.Photos
Imports Google.GData.Client
Imports Google.GData.Extensions
Imports Google.GData.Extensions.Location

Public Class Settings
    Inherits DnnC_Picasa_SettingsBase

#Region "Base Method Implementations"

    Public Overrides Sub LoadSettings()
        Try
            If (Page.IsPostBack = False) Then
                populateTabsList(lstTabs, PortalSettings, TabId.ToString, PortalId)

                If Settings.Contains("PicasaUserName") And Settings.Contains("PicasaPassWord") Then
                    populateGalleriesList()
                    If (Settings.Contains("PicasaDefaultGallery")) Then ddlDefaultGallery.SelectedValue = Settings("PicasaDefaultGallery")
                    pnlGalleries.Visible = True
                Else
                    pnlGalleries.Visible = False
                End If


                If (Settings.Contains("PicasaUserName")) Then txtPUsername.Text = Settings("PicasaUserName").ToString()
                If (Settings.Contains("PicasaPassWord")) Then txtPPassword.Text = Settings("PicasaPassWord").ToString()
                If (Settings.Contains("PicasaGalleryPage")) Then lstTabs.SelectedValue = Settings("PicasaGalleryPage").ToString()

                If (Settings.Contains("PicasaGalleryTitlePosition")) Then ddlGalleryTitlePosition.SelectedValue = Settings("PicasaGalleryTitlePosition")
                If (Settings.Contains("PicasaDisplayImageCount")) Then chkDisplayImageCount.Checked = Settings("PicasaDisplayImageCount")
                If (Settings.Contains("PicasaGalleryThumbSize")) Then ddlGalleryThumSize.SelectedValue = Settings("PicasaGalleryThumbSize")

                If (Settings.Contains("PicasaImageThumbSize")) Then ddlImageThumbSize.SelectedValue = Settings("PicasaImageThumbSize")
                If (Settings.Contains("PicasaImageTitlePosition")) Then ddlImagePosition.SelectedValue = Settings("PicasaImageTitlePosition")

                If (Settings.Contains("PicasaStyle")) Then txtStyle.Text = Settings("PicasaStyle").ToString()
            End If
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Public Overrides Sub UpdateSettings()
        Try
            Dim objModules As New Entities.Modules.ModuleController
            objModules.UpdateModuleSetting(ModuleId, "PicasaUserName", txtPUsername.Text)
            objModules.UpdateModuleSetting(ModuleId, "PicasaPassWord", txtPPassword.Text)
            objModules.UpdateModuleSetting(ModuleId, "PicasaDefaultGallery", ddlDefaultGallery.SelectedValue)
            objModules.UpdateModuleSetting(ModuleId, "PicasaGalleryPage", lstTabs.SelectedValue)

            objModules.UpdateModuleSetting(ModuleId, "PicasaGalleryTitlePosition", ddlGalleryTitlePosition.SelectedValue)
            objModules.UpdateModuleSetting(ModuleId, "PicasaDisplayImageCount", chkDisplayImageCount.Checked)
            objModules.UpdateModuleSetting(ModuleId, "PicasaGalleryThumbSize", ddlGalleryThumSize.SelectedValue)

            objModules.UpdateModuleSetting(ModuleId, "PicasaImageThumbSize", ddlImageThumbSize.SelectedValue)
            objModules.UpdateModuleSetting(ModuleId, "PicasaImageTitlePosition", ddlImagePosition.SelectedValue)
            objModules.UpdateModuleSetting(ModuleId, "PicasaStyle", txtStyle.Text)
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

#End Region

#Region " Private methods"

    Private Sub populateTabsList(ByVal lstTabs As DropDownList, ByVal PortalSettings As DotNetNuke.Entities.Portals.PortalSettings, ByVal DefaultValue As String, portalId As Integer)
        lstTabs.DataSource = GetPortalTabs(PortalSettings.DesktopTabs, -1, False, True, False, False, True)
        lstTabs.DataTextField = "TabName"
        lstTabs.DataValueField = "TabId"
        lstTabs.DataBind()

        If DefaultValue <> "" Then
            If Not lstTabs.Items.FindByValue(DefaultValue) Is Nothing Then
                lstTabs.SelectedValue = DefaultValue
            End If
        End If

        lstTabs.Items.Insert(0, New ListItem(String.Empty, String.Empty))
        lstTabs.SelectedIndex = 0
    End Sub

    Private Sub populateGalleriesList()
        Dim pService As PicasaService = New PicasaService("dnnc.PicasaImageGallery")
        pService.setUserCredentials(Settings("PicasaUserName"), Settings("PicasaPassWord"))  ' needed when you need to show albums with private visibility
        Dim qry As AlbumQuery = New AlbumQuery(PicasaQuery.CreatePicasaUri(Settings("PicasaUserName")))
        Dim feed As PicasaFeed = pService.Query(qry)

        For Each entry As PicasaEntry In feed.Entries
            ddlDefaultGallery.Items.Add(New ListItem(entry.Title.Text, entry.GetPhotoExtensionValue(Google.GData.Photos.GPhotoNameTable.Id)))
        Next

        ddlDefaultGallery.Items.Insert(0, New ListItem(String.Empty, "-1"))
        ddlDefaultGallery.SelectedIndex = 0
    End Sub

#End Region

End Class